package com.szs.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.szs.mapper.StudentsMapper;
import com.szs.pojo.PageResult;
import com.szs.pojo.Student;
import com.szs.pojo.StudentQueryParam;
import com.szs.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class StudentsServiceImpl implements StudentsService {

    @Autowired
    private StudentsMapper studentsMapper;
    @Override
    public PageResult<Student> studentsPage(StudentQueryParam studentQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        //2.执行查询语句
        List<Student> list = studentsMapper.list(studentQueryParam);
        //3.解析分页结果，封装结果
        Page<Student> p = (Page<Student>) list;
        return new PageResult<Student>(p.getTotal(),p.getResult());
    }

    @Transactional
    @Override
    public void save(Student student) {
        //1.保存学员信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.insert(student);
    }
    /*
    * 根据ID查询信息
    * */
    @Override
    public Student getById(Integer id) {
        return studentsMapper.getById(id);
    }
    /*
    * 修改学员信息
    * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStu(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.updateStu(student);
    }
    /*
     * 根据ID删除学学员信息
     * */
    @Override
    public void delete(List<Integer> ids) {

        studentsMapper.delete(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateVio(Integer id, Integer score) {
        //1.先查找id，数据回显
        studentsMapper.updateVio(id,score);

        //2.然后再修改违纪分数

    }


}
